﻿using System;
using System.Configuration;
using System.Data;
using System.Globalization;
using System.Threading;
using System.Web;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using SysComm;
using SysOperationLayer.DBHandle;
using Resources;
using IPMS.Common;

public partial class OptAgentsAdd : Page, IRequiresSessionState
{
    protected override void InitializeCulture()
    {
        string strCulture = ConfigurationManager.AppSettings["DefaultCulture"].ToString();

        if (Session["Culture"] != null)
        {
            strCulture = Session["Culture"].ToString();
        }

        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(strCulture);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(strCulture);
        base.InitializeCulture();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!SysUtil.IsAdministratorOrNormalUserType())
        {
            base.Response.Redirect("ErrorPage/NotAllowed.aspx");
            return;
        }
        else if (!Page.IsPostBack)
        {
            ((Label)Master.FindControl("lblMstTitle")).Text = " | " + ISitemap.Settings + " | " + ISitemap.MaintenanceFiles.ToString() + " | " + ISitemap.AgentsAdd;
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        clearText();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        AgentsDB objDB = new AgentsDB();

        if (objDB.Insert(txtAgent.Text.Trim(),
                            txtName.Text.Trim(),
                            txtChiName.Text.Trim(),
                            txtAddress.Text.Trim(),
                            txtWebSite.Text.Trim(),
                            txtJobTitle.Text.Trim(),
                            txtContactPerson.Text.Trim(),
                            txtPhone.Text.Trim(),
                            txtFax.Text.Trim(),
                            txtEmailAddress.Text.Trim(),
                            txtContactPerson2.Text.Trim(),
                            txtPhone2.Text.Trim(),
                            txtFax2.Text.Trim(),
                            txtEmailAddress2.Text.Trim(),
                            Page.User.Identity.Name.ToString(),
                            HttpContext.Current.Request.UserHostAddress) == 0)
        {
            new LogDB(objDB.GetDB()).writeSucLog(SysUtil.m_functiong_agent, "Create New Agent Master Success! Agent Code: " + txtAgent.Text.Trim());
            objDB.CloseDB();
            Jscript.AlertAndRedirect("Create New Agent Master Success!", "OptAgents.aspx", Page);
        }
        else
        {
            new LogDB(objDB.GetDB()).writeFaileLog(SysUtil.m_functiong_agent, "Create New Agent Master Failure! Agent Code: " + txtAgent.Text.Trim());
            objDB.CloseDB();
            Jscript.Alert("Create New Agent Master Failure!", Page);
            lblError.Text = objDB.ErrorMessage;
        }
    }

    protected void clearText()
    {
        txtAgent.Text = string.Empty;
        txtName.Text = string.Empty;
        txtChiName.Text = string.Empty;
        txtContactPerson.Text = string.Empty;
        txtAddress.Text = string.Empty;
        txtJobTitle.Text = string.Empty;
        txtPhone.Text = string.Empty;
        txtFax.Text = string.Empty;
        txtPhone2.Text = string.Empty;
        txtContactPerson2.Text = string.Empty;
        txtFax2.Text = string.Empty;
        txtWebSite.Text = string.Empty;
        txtEmailAddress.Text = string.Empty;
        txtEmailAddress2.Text = string.Empty;
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("OptAgents.aspx");
    }
}
